#include "ElementLadder.h"

ElementLadder::ElementLadder(vector3<int> pos)
{
    type=LADDER;
    activated=true;
    isCollidable=false;
    orient=LD_FRONT;
    geometry=AABB(vector3<float>(0,0,0),vector3<float>(1,1,1));
    translate(pos.cast<float>());
}

void ElementLadder::invokeCollisionEvent(Character* c){
    float angle = atan2(c->s.velocity.z,c->s.velocity.x) + M_PI;

    if( ((angle<M_PI/4 || angle > M_PI*7./4.) && orient==LD_RIGHT) ||
        ((angle>M_PI/4 && angle < M_PI*3./4.) && orient==LD_FRONT) ||
        ((angle>M_PI*3./4. && angle < M_PI*5./4.) && orient==LD_LEFT) ||
        ((angle>M_PI*5./4. && angle < M_PI*7./4.) && orient==LD_BACK))
    c->s.velocity=vector3<float>(0,GS->getJumpHeight(),0);
}

void ElementLadder::invokeActivateEvent(){

}

void ElementLadder::invokeUpdateEvent(Character *c){

}

void ElementLadder::activate(){

}

void ElementLadder::deactivate(){

}

void ElementLadder::render(){
    static GLuint dlID =0;


    glPushMatrix();
    glColor3f(0.3,0.3,0);

    glTranslatef(s.position.x,s.position.y,s.position.z);
    glTranslatef(0.5,0,0.5);
    glRotatef(orient*90,0,1,0);

    if(!dlID){
    dlID=glGenLists(1);

    glNewList(dlID,GL_COMPILE_AND_EXECUTE);
        glPushMatrix();
            glTranslatef(-0.5,0,0.5);
            glLineWidth(10);
            Drawing::drawLine2D(0.2,0,0.2,1);
            Drawing::drawLine2D(0.8,0,0.8,1);

            glLineWidth(15);
            Drawing::drawLine2D(0.2,0.2,0.8,0.2);
            Drawing::drawLine2D(0.2,0.4,0.8,0.4);
            Drawing::drawLine2D(0.2,0.6,0.8,0.6);
            Drawing::drawLine2D(0.2,0.8,0.8,0.8);
        glPopMatrix();
    glEndList();

    }else glCallList(dlID);
    glPopMatrix();
}
